﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 窗体阴影效果
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private bool isMaximized = false;

        private void Window_StateChanged(object sender, EventArgs e)
        {
            // 如果窗口最大化，使用动画恢复到原始大小
            if (this.WindowState == WindowState.Maximized)
            {
                AnimateWindowSize(SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight);
                isMaximized = true;
            }
            // 如果窗口被最小化，执行相应的操作
            else if (this.WindowState == WindowState.Minimized)
            {
                // 这里可以选择不做动画，直接最小化
                if (isMaximized)
                {
                    AnimateWindowSize(100, 150); // 恢复到指定的默认大小
                    isMaximized = false;
                }
            }
            else
            {
                // 如果窗口没有最小化或最大化，恢复为原始大小
                if (isMaximized)
                {
                    AnimateWindowSize(800, 450); // 恢复到指定的默认大小
                    isMaximized = false;
                }
            }
        }

        // 用于控制窗口大小动画的函数
        private void AnimateWindowSize(double targetWidth, double targetHeight)
        {
            // 创建宽度动画
            DoubleAnimation widthAnimation = new DoubleAnimation
            {
                From = this.Width,
                To = targetWidth,
                Duration = new Duration(TimeSpan.FromMilliseconds(300))
            };

            // 创建高度动画
            DoubleAnimation heightAnimation = new DoubleAnimation
            {
                From = this.Height,
                To = targetHeight,
                Duration = new Duration(TimeSpan.FromMilliseconds(300))
            };

            // 开始动画
            this.BeginAnimation(Window.WidthProperty, widthAnimation);
            this.BeginAnimation(Window.HeightProperty, heightAnimation);
        }

        // 处理点击按钮的事件，切换窗口大小
        private void ToggleWindowSize(object sender, RoutedEventArgs e)
        {
            if (isMaximized)
            {
                // 恢复到原始大小
                AnimateWindowSize(800, 450); // 或者其他你希望的默认大小
                this.WindowState = WindowState.Normal;
            }
            else
            {
                // 最大化窗口
                AnimateWindowSize(SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight);
                this.WindowState = WindowState.Maximized;
            }
            isMaximized = !isMaximized;
        }

        // 创建并应用一个平滑的动画
        private void AnimateWindowMaximize()
        {
            DoubleAnimation widthAnimation = new DoubleAnimation
            {
                From = this.Width,
                To = SystemParameters.FullPrimaryScreenWidth,
                Duration = new Duration(TimeSpan.FromMilliseconds(300))
            };

            DoubleAnimation heightAnimation = new DoubleAnimation
            {
                From = this.Height,
                To = SystemParameters.FullPrimaryScreenHeight,
                Duration = new Duration(TimeSpan.FromMilliseconds(300))
            };

            this.BeginAnimation(Window.WidthProperty, widthAnimation);
            this.BeginAnimation(Window.HeightProperty, heightAnimation);
        }
    }
}
